home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 034 (1987-11-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 034 (1987-11-15)(Ossowski, Stefan)(DE)(PD).adf / Chess / curses.c < prev    next >
C/C++ Source or Header  |  1989-01-18  |  4KB  |  316 lines

  1. /*  curses.c */
  2.  
  3. #include <ctype.h>
  4.  
  5. /* small curses 
  6.     Bob Leivian -- some of the curses routines are here
  7.     add to them as you see fit -- the basic I/O routines are from
  8.     the micro EMACS routines from FISH disk # 23
  9.  
  10.     Bob Leivian
  11.     2702 W. Curry
  12.     Chandler Az 85224 
  13.     602-820-6859   mot!dover!leivian
  14. */
  15.  
  16. /*
  17.  * Name:    MicroEMACS
  18.  *        AmigaDOS terminal I/O
  19.  * Version:    31
  20.  * Compiler:    Manx Aztec C
  21.  * Created:    19-Apr-86 ...!ihnp4!seismo!ut-sally!ut-ngp!mic
  22.  */
  23. #include    <libraries/dos.h>
  24. #include    <libraries/dosextens.h>
  25. #undef TRUE
  26. #undef FALSE
  27.  
  28. #define    NIBUF    128        /* Probably excessive.        */
  29. #define    NOBUF    512        /* Not too big for 750/730.    */
  30.  
  31. struct    FileHandle    *tty;
  32. struct    FileHandle    *Open();
  33. char    obuf[NOBUF];    /* Output buffer        */
  34. int    nobuf;                /* # of bytes in above        */
  35. char    ibuf[NIBUF];    /* Input buffer            */
  36. int    nibuf;                /* # of bytes in above        */
  37. int    nrow;                /* Terminal size, rows.        */
  38. int    ncol;                /* Terminal size, columns.    */
  39.  
  40. #ifdef    MANX
  41. extern    int    Enable_Abort;
  42. #endif
  43. extern char version[];
  44.  
  45. /*
  46.  * This routine gets called once, to set up the
  47.  * terminal channel.
  48.  */
  49. ttopen()
  50. {
  51.     char WindowName[80];
  52.     
  53.     nrow = 23;
  54.     ncol = 77;
  55.     nobuf = nibuf = 0;
  56. #ifdef    MANX
  57.     Enable_Abort = 0;    /* Disable ^C during file I/O */
  58. #endif
  59.     strcpy(WindowName,"RAW:0/0/640/200/");
  60.     strcat(WindowName, version);
  61.     tty = Open(WindowName, MODE_NEWFILE);
  62.     if (tty == (struct FileHandle *) 0) {
  63.         printf("Can't open window!\n");
  64.         exit(200);
  65.     }
  66. }
  67.  
  68. /*
  69.  * This function gets called just
  70.  * before we go back home to the command interpreter.
  71.  * On the Amiga it closes up the virtual terminal window.
  72.  */
  73. ttclose()
  74. {
  75.     if (tty != (struct FileHandle *) 0L) {
  76.         ttflush();
  77.         Close(tty);
  78.     }
  79.     tty = /*(struct FileHandle *)*/ NULL;
  80. #ifdef MANX
  81.     Enable_Abort = 1;
  82. #endif
  83. }
  84.  
  85. /*
  86.  * Write a character to the display.
  87.  * On the Amiga, terminal output is buffered, and
  88.  * we just put the characters in the big array,
  89.  * after cheching for overflow.
  90.  */
  91. ttputc(c)
  92. {
  93.     if (nobuf >= NOBUF)
  94.         ttflush();
  95.     obuf[nobuf++] = c;
  96. }
  97.  
  98. /*
  99.  * This function does the real work of
  100.  * flushing out buffered I/O on the Amiga. All
  101.  * we do is blast out the block with a write call.
  102.  */
  103. ttflush()
  104. {
  105.     if (nobuf > 0) {
  106.         Write(tty,obuf,(long) nobuf);
  107.         nobuf = 0;
  108.     }
  109. }
  110.  
  111. /*
  112.  * Read a character from the terminal,
  113.  * performing no editing and doing conditional echo 
  114.  */
  115. int do_echo = 1; /* echo flag */
  116.  
  117. ttgetc()
  118. {
  119.     unsigned char c, ignore;    /* must be unsigned! */
  120.  
  121.     ttflush();
  122.  
  123.     Read(tty,&c,1L);
  124.     if (c == '\x9b') {
  125.         Read(tty, &c, 1L);
  126.  
  127.         /* was it a function key  */
  128.         if (isdigit(c))
  129.             Read(tty, &ignore, 1L);
  130.  
  131.         /* return the char with top bit set */
  132.         c |= 0x80;
  133.     } else 
  134.         if (do_echo) 
  135.             ttputc(c);
  136.  
  137.     return ((int) c);
  138. }
  139.  
  140. /*
  141.  * Write a string to the terminal 
  142.  */
  143. ttputs(s)
  144. char *s;
  145. {
  146.     while(*s) ttputc(*s++);
  147.     ttflush();
  148. }
  149.  
  150. char scr_buf[24][80];
  151. int scr_row, scr_col;
  152.  
  153.  
  154. /* this is some the curses routines */
  155.  
  156. standout()
  157. {
  158.     /* standout (in this case by reverse vidio) */
  159.     ttputs("\x9b7m");
  160. }
  161.  
  162. standend()
  163. {
  164.     ttputs("\x9bm");
  165. }
  166.  
  167. move(x,y)
  168. {
  169.     char buf[32];
  170.  
  171.  
  172.     sprintf(buf, "\x9b%d;%dH", x+1, y+1);
  173.     ttputs(buf);
  174.     ttflush();
  175.  
  176.     scr_row = x;
  177.     scr_col = y;
  178. }
  179.  
  180. addstr(s)
  181. char *s;
  182. {
  183.     while(*s) {
  184.         ttputc(*s);
  185.         if(*s == '\n') {
  186.            scr_row++;
  187.            scr_col = 0;
  188.         }
  189.         scr_buf[scr_row][scr_col++] = *s++;
  190.         if(scr_col >= 78)
  191.             break;
  192.     }
  193.     ttflush();
  194. }
  195.  
  196. mvaddstr(x,y,s)
  197. int x,y;
  198. char *s;
  199. {
  200.     move(x,y);
  201.     addstr(s);
  202. }
  203.  
  204. addch(c)
  205. char c;
  206. {
  207.     ttputc(c);
  208.     scr_buf[scr_row][scr_col] = c;
  209.     scr_col++;
  210. }
  211.  
  212. mvaddch(x,y,c)
  213. int x,y;
  214. char c;
  215. {
  216.     move(x, y);
  217.     addch(c);
  218. }
  219.  
  220. inch()
  221. {
  222.     return scr_buf[scr_row] [scr_col];
  223. }
  224.  
  225. refresh()
  226. {
  227. }
  228.  
  229. clrtobot()
  230. {
  231.     int i,j;
  232.  
  233.     ttputs("\x9bJ");
  234.     for (j = scr_col; j < 79; j++)
  235.         scr_buf[scr_row][j] = ' ';
  236.     for (i = scr_row++; i < 23; i++)
  237.         for (j = 0; j < 79; j++)
  238.             scr_buf[i][j] = ' ';
  239. }
  240.  
  241. clrtoeol()
  242. {
  243.     int i;
  244.  
  245.     ttputs("\x9bK");
  246.     for (i = scr_col; i < 79; i++)
  247.         scr_buf[scr_row][i] = ' ';
  248. }
  249.  
  250. clear()
  251. {
  252.     int i,j;
  253.  
  254.     ttputc('\f');
  255.     for (i = 0; i < 23; i++)
  256.         for (j = 0; j < 79; j++)
  257.             scr_buf[i][j] = ' ';
  258. }
  259.  
  260. initscr()
  261. {
  262.     ttopen();
  263.     clear();
  264. }
  265.  
  266. crmode()
  267. {
  268. }
  269.  
  270. noecho()
  271. {
  272.     do_echo = 0;
  273. }
  274.  
  275. echo()
  276. {
  277.     do_echo = 1;
  278. }
  279.  
  280. endwin()
  281. {
  282.     ttclose();
  283. }
  284.  
  285. printw(a,b,c,d,e)
  286. char *a;
  287. char *b;
  288. {
  289.     char buf[132];
  290.     
  291.     sprintf(buf, a,b,c,d,e);
  292.     addstr(buf);
  293. }
  294.  
  295. scanw(fmt, buf)
  296. char *fmt;
  297. char *buf;
  298. {
  299.    char temp[32];
  300.    int i = 0;
  301.    int c;
  302.    
  303. again:
  304.    c = ttgetc();
  305.    if(c == 8 ) { i--; if (i <= 0) i = 0; goto again;}
  306.    if(c > 32 ) {
  307.       temp[i++] = c;
  308.       if ( i < 30) goto again;
  309.    }   
  310.  
  311.    temp[i] = '\0';
  312.  
  313.    sscanf(temp, fmt, buf);
  314. }
  315.  
  316.